package com.loveweinuo;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.loveweinuo.databinding.ActivityPayBinding;
import com.loveweinuo.util.AliPayUtil;
import com.loveweinuo.util.HTTPAPI;
import com.loveweinuo.util.LogUtil;
import com.loveweinuo.util.PayStateCallback;
import com.loveweinuo.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 86178 on 2019/8/9.
 * author mogen
 * 用心，成就一切。
 */

public class PayActivity extends Activity implements View.OnClickListener {

    ActivityPayBinding binding;
    String token, userId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pay);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        token = getIntent().getStringExtra("module_token");
        userId = getIntent().getStringExtra("module_userId");
        ToastUtil.showToast("token-->" + token + "\nuserId-->" + userId);
        binding.tvModuleLogin.setOnClickListener(this);
    }


    private PayStateCallback orderCallback = new PayStateCallback() {
        @Override
        public void onPaySuccess(String describe) {
            //支付成功的处理
            ToastUtil.showToast("支付成功");
        }

        @Override
        public void onPayWatting(String describe) {

        }

        @Override
        public void onPayFailed(String describe) {
            //支付失败的处理
            ToastUtil.showToast("支付失败");
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvModuleLogin:
                String url = binding.etModuleUrl.getText().toString().trim();
                if (TextUtils.isEmpty(url)) {
                    ToastUtil.showToast("请输入支付url");
                    return;
                }
                pay(url);
                break;
            default:
        }
    }

    public void pay(String url) {
        HTTPAPI.getInstance().pay(token, userId, url, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ToastUtil.showToast("服务器返回支付信息成功-->" + s);
                binding.tvModuleResult.setText(s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    AliPayUtil.getInstance(PayActivity.this, orderCallback)
                            .startPay((String) jsonObject.get("result"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                binding.tvModuleResult.setText(response.body().toString());
                ToastUtil.showToast("服务器返回支付信息失败-->" + e.getMessage());
            }
        });
    }
}
